import Vue from "vue";
import VueRouter from "vue-router";
import { getToken } from '../utils/auth'

//防止因重复跳转页面引起的报错
import Router from 'vue-router';
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
	return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter);

const routes = [
	{
		path: '/',
		component: () => import('@/views/Main.vue'),
		children: [
			{
				path: '/',
				name: 'home',
				component: () => import('@/views/Home/Home.vue')
			},
			{
				path: '/video',
				name: 'video',
				component: () => import('@/views/VideoManage/VideoManage.vue')
			},
			{
				path: '/user',
				name: 'user',
				component: () => import('@/views/UserManage/UserManage.vue')
			},
			{
				path: '/page1',
				name: 'page1',
				component: () => import('@/views/Other/PageOne.vue')
			},
			{
				path: '/page2',
				name: 'page2',
				component: () => import('@/views/Other/PageTwo.vue')
			},

		]

	},

	{
		path: '/login',
		name: 'login',
		component: () => import('@/views/Login/Login')
	},
	{
		path: '*',
		name: 'login',
		component: () => import('@/views/Login/Login')
	},

];

const router = new VueRouter({
	routes,
});


//路由守卫  to,from,next
router.beforeEach((to, from, next) => {
	let token = getToken()
	if (!token && to.name !== 'login') {
		next({ name: 'login' })
	} else {
		next()
	}
})



export default router;
